<?php
namespace app\spadmin\controller\trade;
use app\common\controller\Backend;
/**
 * 消费记录
 */
class Expense extends Backend{

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('RecordExpense');

    }

    public function index(){

        $this->request->filter(['strip_tags', 'trim']);
        if($this->request->isAjax()){
            $data = $this->request->param();
            if (isset($data['keywords']) && $data['keywords']) {
                $search = model('Account')->where('account_phone|account_card',$data['keywords'])->value('id');
                if($search>0){
                    $map['user_id'] = $search;
                }
            }
            if (isset($data['device_name']) && $data['device_name']) {
                $search = model('Device')->where('device_name',$data['device_name'])->value('id');
                if($search>0){
                    $map['device_id'] = $search;
                }
            }  
            if(isset($data['start_date']) &&  $data['start_date']){
                $map['create_time'] = ['BETWEEN',[strtotime($data['start_date'].' 00:00:00'), strtotime($data['end_date'].' 23:59:59')]];
            }
            $map['status'] = 1;         
            $list =   $this->model->where($map)->order('id desc')->paginate($data['limit'])->toArray();
            foreach ($list['data'] as $key => &$value) {
                if($value['account_id']){
                    $value['user'] = model('Account')->where('id',$value['account_id'])->value('account_phone');
                    $value['card'] = model('Account')->where('id',$value['account_id'])->value('account_card');
                }else{
                    $value['user'] ='';
                    $value['card'] ='';
                }
                if($value['device_id']){
                    $value['device_name'] = model('Device')->where('id',$value['device_id'])->value('device_name');
                }else{
                    $value['device_name'] ='';
                }
                if($value['order_id']){
                    $value['order'] = model('OrderExpense')->where('id',$value['order_id'])->value('order_sn');
                }else{
                    $value['order'] ='';
                }
                
            }
            return $this->result($list);
        }
        return $this->view->fetch();
    }
}